<template>
  <view class="search-container">
    <view class="search-box">
      <uni-search-bar
        :radius="100"
        @input="input"
        @clear="searchList = []"
        cancelButton="none"
      ></uni-search-bar>
    </view>
    <!-- 搜索时显示列表 -->
    <view class="sugg-list" v-if="searchList.length > 0">
      <view class="sugg-item" v-for="item in searchList" :key="item.goods_id">
        <text class="goods-name" @click="goToDetail(item.goods_id)">{{
          item.goods_name
        }}</text>
      </view>
    </view>
    <!-- 历史 -->
    <view v-else class="history-box">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" @click="clear" />
      </view>
      <view class="history-list">
        <uni-tag
          v-for="(item, index) in historyList"
          :key="index"
          :text="item"
          @click="goGoodsList(item)"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
import uniIcons from '../../components/uni-icons/uni-icons.vue'
export default {
  components: { uniIcons },
  data () {
    return {
      searchList: [],
      historyList: [],
      timer: undefined
    }
  },
  onLoad (options) {
    //   调用本地历史数据
    this.historyList = uni.getStorageSync('history') || []
  },
  methods: {
    //   点击跳转商品列表
    goGoodsList (item) {
      uni.navigateTo({ url: '/subpkg/goods-list/index?query=' + item })
    },
    //   输入事件
    input (e) {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.getSearchData(e)
      }, 500)
    },
    async getSearchData (keywords) {
      // 判断是不是空
      if (keywords.trim().length === 0) return
      //   调用接口
      const res = await uni.$request({
        url: 'goods/qsearch',
        data: {
          query: keywords
        }
      })
      //   console.log(res);
      if (res.meta.status === 200) {
        this.searchList = res.message
      }
      //   调用保存历史方法
      this.saveHistory(keywords)
    },
    // 保存历史
    saveHistory (keywords) {
      // 判断重复
      const index = this.historyList.findIndex(item => item === keywords)
      // 截取重复的数据
      if (index > -1) {
        this.historyList.splice(index, 1)
      }
      // 新数据加进第一个
      this.historyList.unshift(keywords)
      // 数据存储本地
      uni.setStorageSync('history', this.historyList)
    },
    // 清空
    clear () {
      this.historyList = []
      uni.setStorageSync('history', [])
    },
    // 跳转商品详情页面
    goToDetail (id) {
      uni.navigateTo({ url: '/subpkg/goods-detail/index?cid=' + id })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #f00;
    }
  }
  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
}
</style>
